package customControls;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.jaden.washer.R;

import java.util.List;

import dormitory.Dormitory;
import order.Order;

public class DormitoryAdapter extends ArrayAdapter {
    Dormitory dormitory;
    int Position;
    private int resourceId;
    ;
    public  DormitoryAdapter(@NonNull Context context, int resource, List<Dormitory> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }
    public class ViewHolder
    {
        TextView  dormlocation;
        TextView freenumber;
        TextView isopen;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Position=position;
        //根据position 得到当前子项的Commodity实例
        dormitory= (Dormitory) getItem(position);
        ViewHolder viewHolder = null;
        if(convertView==null)
        {
            //Log.d(TAG, "getView: "+position);
            //为当前子项加载指定布局->resourceId
            convertView= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder=new ViewHolder();
            //获取控件实例
            getInstance(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        //显示控件信息
        showInformation(viewHolder);
        return convertView;
    }

    //获取控件实例
    private void getInstance(ViewHolder viewHolder, View view) {
        viewHolder.dormlocation=(TextView)view.findViewById(R.id.textView_dormloc_dormlistviewitem);
        viewHolder.freenumber=(TextView)view.findViewById(R.id.textView_freenumber_dormlistviewitem);
        viewHolder.isopen=(TextView)view.findViewById(R.id.textView_isopen_dormlistviewitem);

    }
    //显示控件信息
    @SuppressLint("ResourceAsColor")
    private void showInformation(ViewHolder viewHolder) {
        viewHolder.dormlocation.setText(dormitory.getDormLocation());
        viewHolder.freenumber.setText(dormitory.getFreeNumber()+"台空闲");
        if(dormitory.getFreeNumber()<=0)
            viewHolder.freenumber.setTextColor(R.color.colorred);
        else
            viewHolder.freenumber.setTextColor(R.color.royalblue);
        if(dormitory.getIsOpen()==true) {
            viewHolder.isopen.setText("营业中");
            viewHolder.isopen.setBackgroundColor(getContext().getResources().getColor(R.color.lawngreen));
        }
        else
        {
            viewHolder.isopen.setText("未营业");
            viewHolder.isopen.setBackgroundColor(getContext().getResources().getColor(R.color.colorred));
        }
    }

}
